import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/model/article_model.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/model/user_model.dart';
import 'package:village_android/pages/browser.dart';
import 'package:village_android/utils/http_utlis.dart';

class UserListPage extends StatefulWidget {
  int deptId;

  UserListPage({Key key,this.deptId}) : super(key: key);

  @override
  _UserListPageState createState() => _UserListPageState();
}
//NewsCell

class _UserListPageState extends State<UserListPage> {
  List items = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() {

    Map<String, dynamic> param = new Map();
    if(widget.deptId!=null){
      param["deptId"] = widget.deptId.toString();
    }

    HttpUtils.get("/mgr/list",
        data: param, success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text("用户列表"),
        ),
        body:  items.isEmpty
            ? Center(child: Text('无数据'))
            : Center(
          child: ListView.separated(
              itemBuilder: (context, index) {
                UserModel userModel = UserModel.fromJson(items[index]);
                return _newsCell(context, userModel);
              },
              separatorBuilder: (context, index) =>
                  Divider(height: 1.0, color: Colors.black12),
              itemCount: items.length),
        ),
      ),
    );
  }
  void onItemClick(item) {

    Navigator.of(context).pop(item);

  }


  //独立Item
  Widget _newsCell(BuildContext context, UserModel item) {
    return new GestureDetector(
      onTap: () {onItemClick(item);},
      child: Container(
        height: ScreenUtil().setHeight(150),
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(10.0),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            Image.network(
              item.avatar,
              width: ScreenUtil().setWidth(100),
              height: ScreenUtil().setHeight(100),
              fit: BoxFit.fill,
            ),
            Padding(padding: EdgeInsets.only(left: 15)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Text(
                    item.name,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "地址:　" +item.address,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black54,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
      ),
    );
  }
}
